'use strict';

angular.module('com.dcos.app').controller('TemplateManageController', function($window,$scope,$state,$uibModal, $filter, api, dialog, TemplateService) {
  var vm = this;
  vm.breadCrumbArr = [api.breadCrumbMap.templatemanage.root];
  vm.initPageSize = api.initPageSize;

  vm.templateData=[];

  vm.pipelineData=[];
  
  vm.curProjectId=$window.sessionStorage.getItem("curProjectId");

  vm.userid=$window.sessionStorage.getItem("curUserId");

  // if(!vm.userid)
  //   $state.go('login');

  vm.msg="删除操作不可恢复，确定要删除？";

  vm.getList = function () {
    
    vm.loading = false;
    
    TemplateService.list(vm.curProjectId).then(function (response) {
      if(response.data.templateList!=null)
        vm.templateData=response.data.templateList;
      else
        vm.templateData=[];
      vm.page = 1;

      vm.pageSize = vm.initPageSize;
      vm.totalCount = vm.templateData.length;
      vm.itemLength = vm.totalCount;

      vm.sortKey = "create_time";
      vm.reverse = false;
    });

  }

  vm.getList();


  //创建
  vm.create = function() {
    var modalInstance = $uibModal.open({
      animation: true,
      templateUrl: 'controllers/templatemanage/create/create.html',
      controller: 'TemplateCreateController as vm',
      resolve: {
        reload:function(){return vm.getList;}
      }
    });
  }

  vm.changePager = function () {
    var filtered = $filter('filter')(vm.pipelineData, {name: vm.query});
    vm.totalCount = filtered.length;
  }

  vm.add = function () {
    $state.go('app.templatemanage.detail', {'t_id':null});
  }
  vm.edit = function (t_id) {
    $state.go('app.templatemanage.detail', {'t_id':t_id});
  }
  $scope.ok = function(t_id) {
    TemplateService.delete(t_id).then(function (response) {
      vm.getList();
    });
  }
});

